package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jdy.haoduoaiteacher.activity.BaseActivity;
import com.jdy.haoduoaiteacher.model.NormalStoryitemVo;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

// 普通故事详情 item
@SuppressLint("InflateParams")
public class NormalStoryItemAdapter extends BaseAdapter implements
    OnClickListener {

  private BaseActivity mContext;
  private LayoutInflater mInflater;
  private ArrayList<NormalStoryitemVo> mList;

  public NormalStoryItemAdapter(BaseActivity context,
      ArrayList<NormalStoryitemVo> list) {
    this.mContext = context;
    mInflater = LayoutInflater.from(mContext);
    this.mList = list;
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int arg0) {
    return mList.get(arg0);
  }

  @Override
  public long getItemId(int arg0) {
    return arg0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    NormalStoryitemVo storyitemVo = mList.get(position);
    final ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.item_normalstory, null);
      holder.number = (TextView) convertView.findViewById(R.id.number);
      holder.diamondicon = (ImageView) convertView
          .findViewById(R.id.diamondimg);
      holder.playtag = (ImageView) convertView.findViewById(R.id.playtag);
      holder.storyname = (TextView) convertView.findViewById(R.id.storyname);
      holder.descrtion = (TextView) convertView.findViewById(R.id.description);
      holder.morebtn = (ImageButton) convertView.findViewById(R.id.more);

      holder.layout_other = (LinearLayout) convertView
          .findViewById(R.id.layout_other);
      holder.add_to = (LinearLayout) convertView.findViewById(R.id.item_add);
      holder.story_list = (LinearLayout) convertView
          .findViewById(R.id.item_list);
      holder.download_to = (LinearLayout) convertView
          .findViewById(R.id.item_download);
      holder.love_to = (LinearLayout) convertView.findViewById(R.id.item_love);
      holder.talk_to = (LinearLayout) convertView.findViewById(R.id.item_talk);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.number.setText(storyitemVo.no + "");
    if (storyitemVo.isNeedVip) {
      holder.diamondicon.setVisibility(View.VISIBLE);
    } else {
      holder.diamondicon.setVisibility(View.GONE);
    }
    holder.storyname.setText(storyitemVo.title);
    holder.descrtion.setText(storyitemVo.descrtion);

    holder.morebtn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        if (holder.layout_other.isShown()) {
          holder.layout_other.setVisibility(View.GONE);
        } else {
          holder.layout_other.setVisibility(View.VISIBLE);
        }
      }
    });
    setListener(holder, storyitemVo);
    return convertView;
  }

  private void setListener(ViewHolder holder, NormalStoryitemVo to) {
    holder.add_to.setTag(to);
    holder.add_to.setOnClickListener(this);
    holder.story_list.setTag(to);
    holder.story_list.setOnClickListener(this);
    holder.download_to.setTag(to);
    holder.download_to.setOnClickListener(this);
    holder.love_to.setTag(to);
    holder.love_to.setOnClickListener(this);
    holder.talk_to.setTag(to);
    holder.talk_to.setOnClickListener(this);
  }

  private static class ViewHolder {
    private TextView number; // 序号
    private ImageView diamondicon; // 会员钻石
    @SuppressWarnings("unused")
    private ImageView playtag; // 正在播放是显示的 icon
    private TextView storyname;
    private TextView descrtion;
    private ImageButton morebtn; // 更多

    private LinearLayout layout_other; // 下弹 窗口
    private LinearLayout add_to;
    private LinearLayout story_list;
    private LinearLayout download_to;
    private LinearLayout love_to;
    private LinearLayout talk_to;
  }

  @Override
  public void onClick(View v) {
    NormalStoryitemVo item = (NormalStoryitemVo) v.getTag();

    switch (v.getId()) {
    case R.id.item_add:
      Toast.makeText(mContext, "添加到故事单" + item.no, Toast.LENGTH_SHORT).show();
      break;
    case R.id.item_list:
      Toast.makeText(mContext, "故事单" + item.no, Toast.LENGTH_SHORT).show();
      break;
    case R.id.item_download:
      Toast.makeText(mContext, "下载" + item.no, Toast.LENGTH_SHORT).show();
      break;
    case R.id.item_love:
      Toast.makeText(mContext, "喜欢" + item.no, Toast.LENGTH_SHORT).show();
      break;
    case R.id.item_talk:
      Toast.makeText(mContext, "对话" + item.no, Toast.LENGTH_SHORT).show();
      break;
    default:
      break;
    }
  }

}
